<?php /* CALENDAR $Id: index.php,v 1.30 2005/03/18 08:35:50 gregorerhardt Exp $ */
$AppUI->savePlace();

dPsetMicroTime();

require_once( $AppUI->getModuleClass( 'companies' ) );
require_once( $AppUI->getModuleClass( 'tasks' ) );

// retrieve any state parameters
if (isset( $_REQUEST['company_id'] )) {
	$AppUI->setState( 'CalIdxCompany', intval( $_REQUEST['company_id'] ) );
}
$company_id = $AppUI->getState( 'CalIdxCompany', $AppUI->user_company);

// Using simplified set/get semantics. Doesn't need as much code in the module.
$event_filter = $AppUI->checkPrefState('CalIdxFilter', @$_REQUEST['event_filter'], 'EVENTFILTER', 'my');

// get the passed timestamp (today if none)
$date = dPgetParam( $_GET, 'date', null );

// get the list of visible companies
$company = new CCompany();
$companies = $company->getAllowedRecords( $AppUI->user_id, 'company_id,company_name', 'company_name' );
$companies = arrayMerge( array( '0'=>$AppUI->_('All') ), $companies );

#echo '<pre>';print_r($events);echo '</pre>';
// setup the title block
$titleBlock = new CTitleBlock( 'Monthly Calendar' );
if ($AppUI->user_id != 1) {
	$titleBlock->addCell( $AppUI->_('Company').':' );
	$titleBlock->addCell(
		arraySelect( $companies, 'company_id', 'onChange="document.pickCompany.submit()" class="text"', $company_id ), '',
		'<form action="' . $_SERVER['REQUEST_URI'] . '" method="post" name="pickCompany">', '</form>'
	);
	$titleBlock->addCell();
	$titleBlock->addCell( $AppUI->_('View') . ':');
	$titleBlock->addCell(
		arraySelect($event_filter_list, 'event_filter', 'onChange="document.pickFilter.submit()" class="text"',
		$event_filter, true ), '', "<Form action='{$_SERVER['REQUEST_URI']}' method='post' name='pickFilter'>", '</form>'
	);
}
$titleBlock->show();

if ($AppUI->user_id != 1) {
?>
<script language="javascript">
function clickDay( uts ) {
	go('index.php?m=calendar&a=day_view&date='+uts+'&tab=0');
}
function clickWeek( uts ) {
	go('index.php?m=calendar&a=week_view&date='+uts);
}
</script>
<? } else { ?>
<script language="javascript">
var set = 1;
var dayCell = null;
function setHolidayInfo( day, description, cell ) {
	set = description != '' ? 0 : 1;
	dayCell = cell;
	$('idHoliday').innerHTML = '<b>'+day+'</b>';
	$('idDescription').value = unescape(description);
	Dialog('idSetHoliday').show();
	$('idGlass').onclick = function() { Dialog('idSetHoliday').hide(); }
	$('idDescription').focus();
}

function setHoliday() {
	var day = $('idHoliday').innerHTML;
	var desc = $('idDescription').value;
	var dd, dsrc = day.substr(3, day.indexOf('</b>') - 3);
	day = dsrc.split('/');
	dd = new Date();
	dd.setTime(Date.parse(day[2]+'/'+day[1]+'/'+day[0]));
	if (parseInt(day[1],10) < 10) day[1] = '0'+day[1];
	if (parseInt(day[0],10) < 10) day[0] = '0'+day[0];
	day = day[2] + '-' + day[1] + '-' + day[0];

	ajax.send('index.php?m=calendar&a=setholiday&suppressHeaders=1&set='+set+'&day='+escape(day)+'&desc='+escape(desc), '',
		function(r) {
			if (r != 'OK') {
				alert(r);
				return;
			}
			var s = dayCell.innerHTML;
			s = s.substr(0, s.indexOf('</span>')+7);
			s += '<br/><b>' + desc.replace("\n", '<br/>') + '</b>';
			dayCell.innerHTML = s;
			dayCell.className = desc != '' ? 'holiday clickable' : 
				(dd.getDay() == 0 || dd.getDay() == 6 ? 'weekend clickable' : 'day clickable');
			dayCell.onclick = new Function("setHolidayInfo('"+dsrc+"','"+escape(desc)+"',this)");
		}
	);
	Dialog('idSetHoliday').hide();
}
</script>
<div id="idSetHoliday" class="popup" style="visibility: hidden">
	<table>
		<tr><td id="idHoliday">&nbsp;</td></tr>
		<tr><td><textarea id="idDescription" style="width: 300px; height: 150px;"></textarea></td></tr>
		<tr><td align="center">
			<input type="button" value="Set" onclick="setHoliday()"/>&nbsp;
			<input type="button" value="Cancel" onclick="Dialog('idSetHoliday').hide()"/>
		</td></tr>
	</table>
</div>
<? } ?>
<table cellspacing="0" cellpadding="0" border="0" width="100%"><tr><td>
<?php
// establish the focus 'date'
$date = new CDate( $date );
$cal = new CMonthCalendar( $date  );
$cal->setStyles( 'minical', 'mocal' );

if ($AppUI->user_id != 1) {
	// prepare time period for 'events'
	$first_time = new CDate( $date );
	$first_time->setDay( 1 );
	$first_time->setTime( 0, 0, 0 );
	$first_time->subtractSeconds( 1 );
	$last_time = new CDate( $date );
	$last_time->setDay( $date->getDaysInMonth() );
	$last_time->setTime( 23, 59, 59 );

	$links = array();

	// assemble the links for the tasks
	require_once( dPgetConfig( 'root_dir' )."/modules/calendar/links_tasks.php" );
	getTaskLinks( $first_time, $last_time, $links, 20, $company_id );

	// assemble the links for the events
	require_once( dPgetConfig( 'root_dir' )."/modules/calendar/links_events.php" );
	getEventLinks( $first_time, $last_time, $links, 20 );

	// create the main calendar
	$cal->setLinkFunctions( 'clickDay', 'clickWeek' );
	$cal->setEvents( $links );
}
else {
	$cal->setHolidayHandler( 'setHolidayInfo' );
}

echo $cal->show();
//echo '<pre>';print_r($cal);echo '</pre>';

// create the mini previous and next month calendars under
$minical = new CMonthCalendar( $cal->prev_month );
$minical->setStyles( 'minititle', 'minical' );
$minical->showArrows = false;
$minical->showWeek = false;
$minical->clickMonth = true;
$minical->setLinkFunctions( 'clickDay' );
?>
<table cellspacing="2" cellpadding="0" border="0" width="100%">
	<tr>
		<td valign="top" align="center" width="200" class="minicalb"><?=$minical->show()?></td>
		<td valign="top" align="center" width="100%">&nbsp;<?=$minical->setDate( $cal->next_month );?></td>
		<td valign="top" align="center" width="200" class="minicalb"><?=$minical->show()?></td>
	</tr>
</table>

</td></tr></table>
